function validar()
{
    form = document.getElementById("formulario-login");
    if(form.nick.value == "")
    {
        alert ("El campo Nombre de usuario es obligatorio");
        form.nick.focus();
        return false;
    }
    if(form.pass.value == "")
    {
        alert ("El campo Contraseña es obligatorio");
        form.pass.focus();
        return false;  
    }            
    return true;
}        

//Al cargarse la pagina actuamos
$('#error').hide();//escondemos campo de avisos
$('#formulario-login').submit(
    function(evento)
    {
        evento.preventDefault();
        var error = !validar();
        if(!error)
        {
            $('#error').text("Procesando").fadeIn("slow");
            var datos_formulario = $(this).serialize(); 
            $.ajax(
            {
                url: 'procesarLogin.php',
                data: datos_formulario,
                type: 'POST',
                dataType: 'json',
                success: function(datos){
                    SuccesProcesarLogin(datos);
                }
            });
         }
    }
);
    
function SuccesProcesarLogin(datos){
    $('.error').text(datos.mensaje).fadeIn("slow");
    if(datos.error==false){
        $('.error').text(datos.mensaje).fadeIn("slow");
        //$("#formulario-login").attr("action","aceptarLogin.php");
        //$("#formulario-login").attr("method","POST");
        form=document.getElementById("formulario-login");
        var aux= new Array();
        aux["nick"]=form.nick.value;
        aux["pass"]=form.pass.value;
        post_to_url("aceptarLogin.php", aux);
        //document.forms['formulario-login'].submit();
    }else{
        $('.error').text(datos.mensaje).fadeIn("slow");
    }
}


function post_to_url(path, params, method) {
    method = method || "POST"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("id","newform");
    form.setAttribute("name","newform");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }
    document.body.appendChild(form);
    document.forms['newform'].submit();
}